package com.ziyun.pop.modules.storeProductPrice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ziyun.pop.common.utils.UserUtils;
import com.ziyun.pop.modules.storeProductPrice.entity.StoreGoodsCategoryEntity;
import com.ziyun.pop.modules.storeProductPrice.entity.StoreGoodsProductEntity;
import com.ziyun.pop.modules.storeProductPrice.vo.StorePriceVo;
import io.renren.common.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import com.ziyun.pop.modules.storeProductPrice.dao.StoreProductPriceDao;
import com.ziyun.pop.modules.storeProductPrice.entity.StoreProductPriceEntity;
import com.ziyun.pop.modules.storeProductPrice.service.StoreProductPriceService;
import org.springframework.transaction.annotation.Transactional;


@Service("storeProductPriceService")
public class StoreProductPriceServiceImpl extends ServiceImpl<StoreProductPriceDao, StoreProductPriceEntity> implements StoreProductPriceService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<StoreProductPriceEntity> page = (Page<StoreProductPriceEntity>) this.page(
                new Query<StoreProductPriceEntity>(params).getPage()
                ,new QueryWrapper<StoreProductPriceEntity>()
        );
        return new PageUtils(page);
    }

    /**
     * 根据分类id找所有的商品,如果该门店配置了价格,则将价格查出展示
     * @param catId
     * @return
     */
    @Override
    public List<StoreGoodsProductEntity> getStoreProductByCatId(Integer catId,Long sellerId) {
        return this.baseMapper.getStoreProductByCatId(catId,sellerId);
    }

    @Override
    public void flagDelete(Integer[] stepIds) {
        this.baseMapper.flagDelete(StringUtils.join(stepIds,","));
    }

    //编辑前先把以前的标记删除
    @Override
    public void editFlagDelete(Integer productId, Long sellerId) {
        this.baseMapper.editFlagDelete(productId,sellerId);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public R editPrice(StorePriceVo storePriceVo) {
        String stepPrice = storePriceVo.getStepPrice();
        StoreGoodsProductEntity storeGoodsProductEntity = storePriceVo.getStoreGoodsProductEntity();
        //需要先把该门店下的该产品标记删除,再添加
        this.editFlagDelete(storeGoodsProductEntity.getProductId(),UserUtils.getCurentUser().getSellerId());
        String marks = storePriceVo.getMarks();
        StoreProductPriceEntity storeProductPriceEntity = new StoreProductPriceEntity();
        storeProductPriceEntity.setProductId(storeGoodsProductEntity.getProductId());
        storeProductPriceEntity.setSellerId(UserUtils.getCurentUser().getSellerId());
        storeProductPriceEntity.setPrice(storeGoodsProductEntity.getStorePrice());
        storeProductPriceEntity.setStepPrice(stepPrice);
        storeProductPriceEntity.setStartPrice(storePriceVo.getStartPrice());
        storeProductPriceEntity.setMarks(marks);
        storeProductPriceEntity.setDel(false);
        storeProductPriceEntity.setCreateName(UserUtils.getCurentUser().getUsername());
        boolean save = this.save(storeProductPriceEntity);
        if(save){
            return R.ok();
        }else{
            return R.error("保存价格失败");
        }
    }

    @Override
    public List<StoreGoodsProductEntity> getStoreProductByName(String queryName, Long sellerId) {
        return this.baseMapper.getStoreProductByName(queryName,sellerId);
    }

    /**
     *门店分类树获得
     * @return
     */
    @Override
    public Map<String, Object> getStoreCategoryTree() {
        Map<String, Object> rsMap = new HashMap();
        List<StoreGoodsCategoryEntity> storeCategoryList = this.baseMapper.getStoreCategoryList();
        List<Map> storeCategoryTree = categoryTreeHandle(storeCategoryList, 0);
        rsMap.put("storeCategoryTree",storeCategoryTree);
        return rsMap;
    }



    private List<Map> categoryTreeHandle(List<StoreGoodsCategoryEntity> storeGoodsCategoryEntity, int pid) {
        List<Map> resultList = new ArrayList<>();

        //获取顶级元素集合
        Integer parentId;
        for (StoreGoodsCategoryEntity goodsCategory : storeGoodsCategoryEntity) {
            parentId = goodsCategory.getParentCategoryId();
            if (parentId == null || pid == parentId) {
                Map map = new HashMap();
                map.put("label", goodsCategory.getCategoryName());
                map.put("value", goodsCategory.getCategoryId());
                resultList.add(map);
            }
        }

        //获取每个顶层元素的子数据集合
        for (Map map : resultList) {
            int value = Integer.parseInt(map.get("value").toString());
            List<Map> temp = categoryTreeHandle(storeGoodsCategoryEntity, value);
            if (temp.size() > 0) {
                map.put("children", temp);
            }
        }
        return resultList;
    }

}
